package edu.sctu.wangfuxin.day20150623.httpTest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by user06 on 2015/6/23.
 */
public class HttpConnection {

    public static void getconnection(String path,String param) throws IOException {

        URL url = new URL(path);

        int count;
        byte[] buffer = new byte[1024];

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);


//        urlConnection.setRequestProperty("Content-Type", "text/html");
//        urlConnection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
//        urlConnection.setRequestProperty("Charset", "UTF-8");

        urlConnection.connect();

        OutputStream outputstram =  urlConnection.getOutputStream();
        outputstram.write(param.getBytes());
        outputstram.close();

        InputStream inputStream = urlConnection.getInputStream();

        while((count = inputStream.read(buffer))!=-1){
            System.out.println(new String(buffer,0,count,"utf-8"));

        }
        System.out.println();
        inputStream.close();
    }
}
